﻿using System;
using System.Collections.Generic;
using System.Text;

namespace App39.Behaviors
{
    [Microsoft.Xaml.Interactivity.TypeConstraint(typeof(Windows.UI.Xaml.Controls.TextBox))]
    class SocialTextBoxBehavior : Windows.UI.Xaml.DependencyObject, Microsoft.Xaml.Interactivity.IBehavior
    {
        Windows.UI.Xaml.Controls.TextBox _TextBox;
        readonly string pattern = "http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?";

        public Windows.UI.Xaml.DependencyObject AssociatedObject { get; set; }
        [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
        public void Attach(Windows.UI.Xaml.DependencyObject associatedObject)
        {
            this.AssociatedObject = this._TextBox = associatedObject as Windows.UI.Xaml.Controls.TextBox;
            this._TextBox.TextChanged += _TextBox_TextChanged;
            this._TextBox.KeyDown += _TextBox_KeyDown;
            this._TextBox.Paste += _TextBox_Paste;
        }

        public void Detach()
        {
            this._TextBox.TextChanged -= _TextBox_TextChanged;
            this._TextBox.KeyDown -= _TextBox_KeyDown;
            this._TextBox.Paste -= _TextBox_Paste;
        }

        void _TextBox_Paste(object sender, Windows.UI.Xaml.Controls.TextControlPasteEventArgs e)
        {
            this._TextBox_KeyDown(this._TextBox, null);
            e.Handled = !this.IsValid;
        }

        void _TextBox_TextChanged(object sender, Windows.UI.Xaml.Controls.TextChangedEventArgs e)
        {
            this._TextBox_KeyDown(this._TextBox, null);
        }

        void _TextBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
        {
            var length = this._TextBox.Text.Length;
            var matches = System.Text.RegularExpressions.Regex
                .Matches(this._TextBox.Text, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            foreach (var match in matches)
            {
                length -= match.ToString().Length;
                length += this.UrlLength;
            }
            this.CurrentLength = length;
            if (e != null)
                e.Handled = (length >= this.MaxLength);
            this.RemainingLength = this.MaxLength - length;
            this.IsValid = (this.CurrentLength >= this.MinLength) && (this.RemainingLength >= 0);
            if (this.RemainingLength < 0)
            {
                var lines = System.Text.RegularExpressions.Regex.Matches(this._TextBox.Text, @"\r\n").Count;
                this._TextBox.Select(this.MaxLength - lines, Math.Abs(this.RemainingLength));
            }
        }

        public int CurrentLength { get { return (int)GetValue(CurrentLengthProperty); } private set { SetValue(CurrentLengthProperty, value); } }
        public static readonly Windows.UI.Xaml.DependencyProperty CurrentLengthProperty =
            Windows.UI.Xaml.DependencyProperty.Register("CurrentLength", typeof(int),
            typeof(SocialTextBoxBehavior), new Windows.UI.Xaml.PropertyMetadata(0));

        public int RemainingLength { get { return (int)GetValue(RemainingLengthProperty); } private set { SetValue(RemainingLengthProperty, value); } }
        public static readonly Windows.UI.Xaml.DependencyProperty RemainingLengthProperty =
            Windows.UI.Xaml.DependencyProperty.Register("RemainingLength", typeof(int),
            typeof(SocialTextBoxBehavior), new Windows.UI.Xaml.PropertyMetadata(140));

        public int MinLength { get { return (int)GetValue(MinLengthProperty); } set { SetValue(MinLengthProperty, value); } }
        public static readonly Windows.UI.Xaml.DependencyProperty MinLengthProperty =
            Windows.UI.Xaml.DependencyProperty.Register("MinLength", typeof(int),
            typeof(SocialTextBoxBehavior), new Windows.UI.Xaml.PropertyMetadata(5));

        public int MaxLength { get { return (int)GetValue(MaxLengthProperty); } set { SetValue(MaxLengthProperty, value); } }
        public static readonly Windows.UI.Xaml.DependencyProperty MaxLengthProperty =
            Windows.UI.Xaml.DependencyProperty.Register("MaxLength", typeof(int),
            typeof(SocialTextBoxBehavior), new Windows.UI.Xaml.PropertyMetadata(140));

        public int UrlLength { get { return (int)GetValue(UrlLengthProperty); } set { SetValue(UrlLengthProperty, value); } }
        public static readonly Windows.UI.Xaml.DependencyProperty UrlLengthProperty =
            Windows.UI.Xaml.DependencyProperty.Register("UrlLength", typeof(int),
            typeof(SocialTextBoxBehavior), new Windows.UI.Xaml.PropertyMetadata(23));

        public bool IsValid { get { return (bool)GetValue(IsValidProperty); } set { SetValue(IsValidProperty, value); } }
        public static readonly Windows.UI.Xaml.DependencyProperty IsValidProperty =
            Windows.UI.Xaml.DependencyProperty.Register("IsValid", typeof(bool),
            typeof(SocialTextBoxBehavior), new Windows.UI.Xaml.PropertyMetadata(true));
    }
}
